Skip to content

Conversation

vitalybuka
Copy link
Collaborator

Similar to #107332.

Created using spr 1.3.4
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:codegen IR generation bugs: mangling, exceptions, etc. labels Nov 4, 2024
@llvmbot
Copy link
Member

llvmbot commented Nov 4, 2024

@llvm/pr-subscribers-clang-codegen

Author: Vitaly Buka (vitalybuka)

Changes

Similar to #107332.


Full diff: https://github.com/llvm/llvm-project/pull/114754.diff

4 Files Affected:

  • (modified) clang/docs/ReleaseNotes.rst (+3-4)
  • (modified) clang/docs/SanitizerSpecialCaseList.rst (+7-7)
  • (modified) clang/lib/CodeGen/CGExpr.cpp (+4)
  • (added) clang/test/CodeGen/ubsan-type-ignorelist-enum.test (+33)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index a8e2e5c1e045ce..b2d54821e721e9 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -871,10 +871,9 @@ Sanitizers
   This new flag should allow those projects to enable integer sanitizers with
   less noise.
 
-- Arithmetic overflow sanitizers ``-fsanitize=signed-integer-overflow`` and
-  ``-fsanitize=unsigned-integer-overflow`` as well as the implicit integer
-  truncation sanitizers ``-fsanitize=implicit-signed-integer-truncation`` and
-  ``-fsanitize=implicit-unsigned-integer-truncation`` now properly support the
+- ``-fsanitize=signed-integer-overflow``, ``-fsanitize=unsigned-integer-overflow``,
+  ``-fsanitize=implicit-signed-integer-truncation``, ``-fsanitize=implicit-unsigned-integer-truncation``,
+  ``-fsanitize=enum`` now properly support the
   "type" prefix within `Sanitizer Special Case Lists (SSCL)
   <https://clang.llvm.org/docs/SanitizerSpecialCaseList.html>`_. See that link
   for examples.
diff --git a/clang/docs/SanitizerSpecialCaseList.rst b/clang/docs/SanitizerSpecialCaseList.rst
index 96a7b2fba4ae43..5c88c2976e8613 100644
--- a/clang/docs/SanitizerSpecialCaseList.rst
+++ b/clang/docs/SanitizerSpecialCaseList.rst
@@ -16,8 +16,9 @@ Goal and usage
 ==============
 
 Users of sanitizer tools, such as :doc:`AddressSanitizer`,
-:doc:`ThreadSanitizer`, :doc:`MemorySanitizer` or :doc:`UndefinedBehaviorSanitizer`
-may want to disable or alter some checks for certain source-level entities to:
+:doc:`HardwareAssistedAddressSanitizerDesign`, :doc:`ThreadSanitizer`,
+:doc:`MemorySanitizer` or :doc:`UndefinedBehaviorSanitizer` may want to disable
+or alter some checks for certain source-level entities to:
 
 * speedup hot function, which is known to be correct;
 * ignore a function that does some low-level magic (e.g. walks through the
@@ -51,11 +52,10 @@ Example
 Usage with UndefinedBehaviorSanitizer
 =====================================
 
-The arithmetic overflow sanitizers ``unsigned-integer-overflow`` and
-``signed-integer-overflow`` as well as the implicit integer truncation
-sanitizers ``implicit-signed-integer-truncation`` and
-``implicit-unsigned-integer-truncation`` support the ability to adjust
-instrumentation based on type.
+``unsigned-integer-overflow``, ``signed-integer-overflow``,
+``implicit-signed-integer-truncation``,
+``implicit-unsigned-integer-truncation``, and ``enum`` sanitizers support the
+ability to adjust instrumentation based on type.
 
 By default, supported sanitizers will have their instrumentation disabled for
 types specified within an ignorelist.
diff --git a/clang/lib/CodeGen/CGExpr.cpp b/clang/lib/CodeGen/CGExpr.cpp
index e90e8da3e9f1ea..3388a6df466d45 100644
--- a/clang/lib/CodeGen/CGExpr.cpp
+++ b/clang/lib/CodeGen/CGExpr.cpp
@@ -1941,6 +1941,10 @@ bool CodeGenFunction::EmitScalarRangeCheck(llvm::Value *Value, QualType Ty,
       cast<llvm::IntegerType>(Value->getType())->getBitWidth() == 1)
     return false;
 
+  if (NeedsEnumCheck &&
+      getContext().isTypeIgnoredBySanitizer(SanitizerKind::Enum, Ty))
+    return false;
+
   llvm::APInt Min, End;
   if (!getRangeForType(*this, Ty, Min, End, /*StrictEnums=*/true, IsBool))
     return true;
diff --git a/clang/test/CodeGen/ubsan-type-ignorelist-enum.test b/clang/test/CodeGen/ubsan-type-ignorelist-enum.test
new file mode 100644
index 00000000000000..d041c79bbcafdf
--- /dev/null
+++ b/clang/test/CodeGen/ubsan-type-ignorelist-enum.test
@@ -0,0 +1,33 @@
+// RUN: rm -rf %t
+// RUN: split-file %s %t
+
+// RUN: %clang_cc1 -triple x86_64-linux-gnu -fsanitize=enum -fsanitize-ignorelist=%t/ignorelist -emit-llvm %t/test.cpp -o - | FileCheck %s --implicit-check-not="call void @__ubsan_handle"
+
+//--- ignorelist
+[enum]
+type:IgnoreEnum
+
+//--- test.cpp
+enum IgnoreEnum {
+  A,
+  B,
+  C,
+};
+
+// CHECK-LABEL: define dso_local noundef i32 @_Z6ignore10IgnoreEnum
+int ignore(IgnoreEnum v) {
+  return v;
+}
+
+
+enum CheckEnum {
+  X,
+  Y,
+  Z,
+};
+
+// CHECK-LABEL: define dso_local noundef i32 @_Z5check9CheckEnum
+// CHECK: call void @__ubsan_handle_load_invalid_value_abort
+int check(CheckEnum v) {
+  return v;
+}

@llvmbot
Copy link
Member

llvmbot commented Nov 4, 2024

@llvm/pr-subscribers-clang

Author: Vitaly Buka (vitalybuka)

Changes

Similar to #107332.


Full diff: https://github.com/llvm/llvm-project/pull/114754.diff

4 Files Affected:

  • (modified) clang/docs/ReleaseNotes.rst (+3-4)
  • (modified) clang/docs/SanitizerSpecialCaseList.rst (+7-7)
  • (modified) clang/lib/CodeGen/CGExpr.cpp (+4)
  • (added) clang/test/CodeGen/ubsan-type-ignorelist-enum.test (+33)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index a8e2e5c1e045ce..b2d54821e721e9 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -871,10 +871,9 @@ Sanitizers
   This new flag should allow those projects to enable integer sanitizers with
   less noise.
 
-- Arithmetic overflow sanitizers ``-fsanitize=signed-integer-overflow`` and
-  ``-fsanitize=unsigned-integer-overflow`` as well as the implicit integer
-  truncation sanitizers ``-fsanitize=implicit-signed-integer-truncation`` and
-  ``-fsanitize=implicit-unsigned-integer-truncation`` now properly support the
+- ``-fsanitize=signed-integer-overflow``, ``-fsanitize=unsigned-integer-overflow``,
+  ``-fsanitize=implicit-signed-integer-truncation``, ``-fsanitize=implicit-unsigned-integer-truncation``,
+  ``-fsanitize=enum`` now properly support the
   "type" prefix within `Sanitizer Special Case Lists (SSCL)
   <https://clang.llvm.org/docs/SanitizerSpecialCaseList.html>`_. See that link
   for examples.
diff --git a/clang/docs/SanitizerSpecialCaseList.rst b/clang/docs/SanitizerSpecialCaseList.rst
index 96a7b2fba4ae43..5c88c2976e8613 100644
--- a/clang/docs/SanitizerSpecialCaseList.rst
+++ b/clang/docs/SanitizerSpecialCaseList.rst
@@ -16,8 +16,9 @@ Goal and usage
 ==============
 
 Users of sanitizer tools, such as :doc:`AddressSanitizer`,
-:doc:`ThreadSanitizer`, :doc:`MemorySanitizer` or :doc:`UndefinedBehaviorSanitizer`
-may want to disable or alter some checks for certain source-level entities to:
+:doc:`HardwareAssistedAddressSanitizerDesign`, :doc:`ThreadSanitizer`,
+:doc:`MemorySanitizer` or :doc:`UndefinedBehaviorSanitizer` may want to disable
+or alter some checks for certain source-level entities to:
 
 * speedup hot function, which is known to be correct;
 * ignore a function that does some low-level magic (e.g. walks through the
@@ -51,11 +52,10 @@ Example
 Usage with UndefinedBehaviorSanitizer
 =====================================
 
-The arithmetic overflow sanitizers ``unsigned-integer-overflow`` and
-``signed-integer-overflow`` as well as the implicit integer truncation
-sanitizers ``implicit-signed-integer-truncation`` and
-``implicit-unsigned-integer-truncation`` support the ability to adjust
-instrumentation based on type.
+``unsigned-integer-overflow``, ``signed-integer-overflow``,
+``implicit-signed-integer-truncation``,
+``implicit-unsigned-integer-truncation``, and ``enum`` sanitizers support the
+ability to adjust instrumentation based on type.
 
 By default, supported sanitizers will have their instrumentation disabled for
 types specified within an ignorelist.
diff --git a/clang/lib/CodeGen/CGExpr.cpp b/clang/lib/CodeGen/CGExpr.cpp
index e90e8da3e9f1ea..3388a6df466d45 100644
--- a/clang/lib/CodeGen/CGExpr.cpp
+++ b/clang/lib/CodeGen/CGExpr.cpp
@@ -1941,6 +1941,10 @@ bool CodeGenFunction::EmitScalarRangeCheck(llvm::Value *Value, QualType Ty,
       cast<llvm::IntegerType>(Value->getType())->getBitWidth() == 1)
     return false;
 
+  if (NeedsEnumCheck &&
+      getContext().isTypeIgnoredBySanitizer(SanitizerKind::Enum, Ty))
+    return false;
+
   llvm::APInt Min, End;
   if (!getRangeForType(*this, Ty, Min, End, /*StrictEnums=*/true, IsBool))
     return true;
diff --git a/clang/test/CodeGen/ubsan-type-ignorelist-enum.test b/clang/test/CodeGen/ubsan-type-ignorelist-enum.test
new file mode 100644
index 00000000000000..d041c79bbcafdf
--- /dev/null
+++ b/clang/test/CodeGen/ubsan-type-ignorelist-enum.test
@@ -0,0 +1,33 @@
+// RUN: rm -rf %t
+// RUN: split-file %s %t
+
+// RUN: %clang_cc1 -triple x86_64-linux-gnu -fsanitize=enum -fsanitize-ignorelist=%t/ignorelist -emit-llvm %t/test.cpp -o - | FileCheck %s --implicit-check-not="call void @__ubsan_handle"
+
+//--- ignorelist
+[enum]
+type:IgnoreEnum
+
+//--- test.cpp
+enum IgnoreEnum {
+  A,
+  B,
+  C,
+};
+
+// CHECK-LABEL: define dso_local noundef i32 @_Z6ignore10IgnoreEnum
+int ignore(IgnoreEnum v) {
+  return v;
+}
+
+
+enum CheckEnum {
+  X,
+  Y,
+  Z,
+};
+
+// CHECK-LABEL: define dso_local noundef i32 @_Z5check9CheckEnum
+// CHECK: call void @__ubsan_handle_load_invalid_value_abort
+int check(CheckEnum v) {
+  return v;
+}

@JustinStitt
Copy link
Contributor

Hi Vitaly, thanks for expanding the usefulness of type suppression through SCLs. LGTM

@vitalybuka vitalybuka merged commit 21d25d2 into main Nov 5, 2024
12 checks passed
@vitalybuka vitalybuka deleted the users/vitalybuka/spr/ubsan-suppression-by-type-for-fsanitizeenum branch November 5, 2024 00:00
PhilippRados pushed a commit to PhilippRados/llvm-project that referenced this pull request Nov 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:codegen IR generation bugs: mangling, exceptions, etc. clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants